package com.tong.hao.fw.service.intercept;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName MyHandlerInterceptor
 * @Author 友野浩二
 * @Description 登录拦截器: 使用Spring提供的拦截器(HandlerInterceptor)进行更加精细的控制
 * @Version 1.0
 */
@Component
@Slf4j
public class FwHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("FW preHandle: 在业务处理器处理请求之前被调用,即在调用controller之前调用");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("FW postHandle: 在业务处理器处理请求执行完成后，生成视图之前执行，如果控制器Controller出现了异常，则不会执行此方法");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("FW afterCompletion: 不管有没有异常，请求调用完成后回调方法，即在视图渲染完成后回调");
    }
}
